package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.AsyncTask
import android.provider.MediaStore
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.lifecycle.viewModelScope
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityContactCustomerBinding
import client.comm.baoding.ui.vm.TuiGuangViewModel
import client.comm.baoding.utils.ReadAssetsUtil
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.utils.Util
import client.comm.commlib.widget.InviteDrawWrap
import com.blankj.utilcode.util.PhoneUtils
import com.bumptech.glide.Glide
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.IOException
import java.net.URL
import com.bumptech.glide.request.target.SimpleTarget

import com.bumptech.glide.request.transition.Transition
import com.bumptech.glide.request.target.ViewTarget


class ContactCustomerActivity :
    BaseMiddleActivity<TuiGuangViewModel, ActivityContactCustomerBinding>() {

    //    lateinit var drawWrapRes: InviteDrawWrap
    var lxPhone: String? = null

    val user: UserInfo? by lazy {
        StoreUtil.getBean<UserInfo>(this@ContactCustomerActivity)
    }

    override fun getLayout(): Int {
        return R.layout.activity_contact_customer
    }

    override fun initViewModel(): TuiGuangViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
//        drawWrapRes = binding.drawWrap
    }

    override fun initData() {

        if (user != null) {
            if (!TextUtils.isEmpty(user!!.lx_img)) {
                mImgLoader.load(user!!.lx_img).into(binding.drawWrap)
            }
            if (!TextUtils.isEmpty(user!!.lx_phone)) {
                lxPhone = user!!.lx_phone
            }
        }


    }

    fun fzlj() {
        user?.invite?.let {
            if (!it.isNullOrEmpty()) {
                val cmb = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                cmb.setPrimaryClip(ClipData.newPlainText(null, it))
                toast("推广链接已复制")
            } else {
                toast("推广链接为空复制失败")
            }
        }
    }

    fun wdtd() {
        startActivity(Intent(this, TdActivity::class.java).apply {
            putExtra("bean", StoreUtil.getBean<UserInfo>(this@ContactCustomerActivity)!!.id)
        })
    }

    fun save() {
        viewModel.viewModelScope.launch {
            viewModel.isShowing.value = true
            val r = withContext(Dispatchers.IO) {
                var ret = false
                try {
                    val bitmap = Bitmap.createBitmap(
                        binding.drawWrap.width, binding.drawWrap.height,
                        Bitmap.Config.ARGB_8888
                    )
                    val canvas = Canvas(bitmap)
                    //                            canvas.drawColor(Color.parseColor("#180f48"));
                    binding.drawWrap.draw(canvas)
                    MediaStore.Images.Media.insertImage(
                        contentResolver,
                        bitmap,
                        "邀请",
                        "description"
                    )
                    ret = true
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                ret
            }
            if (r) {
                toast("已保存相册，去微信添加好友吧。")
            } else {
                toast("保存失败")
            }
            viewModel.isShowing.value = false
        }
    }

    /**
     * 联系客服
     */
    fun contactCustomer() {
        lxPhone?.let {
            PhoneUtils.dial(it)
        }

    }

}